#include <stdint.h>
#include <stdio.h>
#include "stm32f1xx_hal.h"

#include "elec_part.h"

typedef struct {
    GPIO_TypeDef *port;
    uint16_t pin;
}limit_switch_t;
typedef struct {
    GPIO_TypeDef *port;
    uint16_t pin;
}led_t;
typedef struct {
    GPIO_TypeDef *port;
    uint16_t pin;
}r_sensor_t;

/* 限位开关 */
limit_switch_t start_limit_sw = {
    GPIOA,
    GPIO_PIN_6,
};
limit_switch_t end_limit_sw = {
    GPIOA,
    GPIO_PIN_7,
};
limit_switch_t low_limit_sw = {
    GPIOA,
    GPIO_Pin_8,
};
/* LED */
led_t start_turn_led = {
    GPIOB,
    GPIO_Pin_5,
};
led_t end_turn_led = {
    GPIOB,
    GPIO_Pin_6,
};

/* 为限位开关处理的回调函数指针，指向上层的处理函数 */
switchCallBack start_limit_switch_callback, end_limit_switch_callback, low_limit_switch_callback;

void start_limit_switch_IT_caller()
{
    HAL_Delay(10);
    // TODO 消抖 if ()
    start_limit_switch_callback();
}
void end_limit_switch_IT_caller()
{
    HAL_Delay(10);
    end_limit_switch_callback();
}
void low_limit_switch_IT_caller()
{
    HAL_Delay(10);
    low_limit_switch_callback();
}

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
   if (GPIO_Pin == start_limit_sw.pin){
       start_limit_switch_IT_caller();
   }
   else if (GPIO_Pin == end_limit_sw.pin){
       end_limit_switch_IT_caller();
   }
   else if (GPIO_Pin == low_limit_sw.pin){
       low_limit_switch_IT_caller();
   }
   
}